package com.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.base.persistence.Page;
import com.common.web.controller.BaseController;
import com.common.web.resp.vo.ResponseInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.system.service.ISysRoleService;
import com.system.vo.SysMenuVO;
import com.system.vo.SysRoleVO;

@Controller
@RequestMapping("/sys/roleMgr")
public class SysRoleController extends BaseController{

	@Autowired
	private ISysRoleService roleService;
	
	@RequestMapping
	public String index(){
		return "view-system/role/role";
	}
	
	@RequestMapping(value="roleList")
	@ResponseBody
	public String roleList(Page<SysRoleVO> page, SysRoleVO vo,
                           HttpServletRequest request) throws Exception {
		List<SysRoleVO> roleList= roleService.getRoleListBypage(page,vo);
		ResponseInfo ri = new ResponseInfo(page,roleList);
		String json = ri.toJson();
		return json;
	}
	
	@RequestMapping(value="roleListByParam")
	@ResponseBody
	public String roleListByParam(Page<SysRoleVO> page, SysRoleVO vo,
                                  HttpServletRequest request) throws Exception {
		String roleGroupId = request.getParameter("group_id");
		List<SysRoleVO> roleList= roleService.findSysRoleVOsBymap(page, roleGroupId, vo.getRoleName());
		ResponseInfo ri = new ResponseInfo(page,roleList);
		String json = ri.toJson();
		return json;
	}
	
	//查询所有roleTypes queryRolesByTypeId
	@RequestMapping(value="roleTypeList")
	@ResponseBody
	public String roleTypeList() throws Exception {
		/*List<SysCodeInfoVO> roleTypeList= roleService.getRoleTypes();
		String orgJson = getListJson(roleTypeList);
		return orgJson;*/
		return null;
	}
	
	//查询所有roleTypes queryRolesByTypeId
	@RequestMapping(value="queryRolesByType")
	@ResponseBody
	public String queryRolesByType(String roleType) throws Exception {
		List<SysRoleVO> SysRoleList= roleService.getRolesByType(roleType);
		String rolesJson = getListJson(SysRoleList);
		return rolesJson;
	}
	
	public String getListJson(List<?> list) throws JsonProcessingException{
		ObjectMapper mapper = new ObjectMapper();
		String jsonList = mapper.writeValueAsString(list);
		return jsonList;
	}
	
	/** zh */
	//查询资源
	@RequestMapping("/queryResource")
	@ResponseBody
	public String queryResourceById(String roleId){
		List<SysMenuVO> menus = new ArrayList<SysMenuVO>();
		menus = roleService.findPermissionsByRoleId(roleId);
		ResponseInfo info = new ResponseInfo();
		info.setInfo(menus);
		return info.toJson();
	}
	
	//删除角色
	@RequestMapping("/deleteRoles")
	@ResponseBody
	public String deleteRoles(String roles){
		String str = StringEscapeUtils.unescapeHtml(roles);
		List<SysRoleVO> list = new ArrayList<SysRoleVO>();
		list = JSON.parseArray(str, SysRoleVO.class);
		List<String> roleIds=new ArrayList<String>();
		ResponseInfo info = new ResponseInfo();
		if(list != null && list.size()>0){
			for (SysRoleVO sysRoleVO : list) {
				roleIds.add(sysRoleVO.getRoleId());
			}
		 	try {
				roleService.deleteRoles(roleIds);
				return info.toJson();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return ResponseInfo.errorJson("删除失败");
			}
		}
		return ResponseInfo.errorJson("删除失败");
	}
	
	//修改角色基本信息
	@RequestMapping("/editRole")
	@ResponseBody
	public String editRole(SysRoleVO role, HttpServletRequest request){
		ResponseInfo info = new ResponseInfo();
		try {
			roleService.updateRole(role);
			return info.toJson();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseInfo.errorJson("修改基本信息失败");
		}
		
	}
	
	//修改角色资源信息
	@RequestMapping("/editRoleResorce")
	@ResponseBody
	public String editRoleResorce(String roleId,String menuIds){
		String menus = StringEscapeUtils.unescapeHtml(menuIds);
		List<String> ids = JSON.parseArray(menus, String.class);
		ResponseInfo info = new ResponseInfo();
		try {
			roleService.createRoleAndMenus( roleId,ids);
			return info.toJson();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseInfo.errorJson("修改资源信息失败");
		}
	}
	
	//新增角色
	@RequestMapping("/createRole")
	@ResponseBody
	public String createRole(SysRoleVO role){
		role.setRoleId(UUID.randomUUID().toString().replaceAll("-", ""));
		ResponseInfo info = new ResponseInfo();
		try {
			roleService.save(role);
			return info.toJson();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseInfo.errorJson("新增失败");
		}
	}
	
	@RequestMapping("/add/Index")
	public String addRoleIndex(){
		return "view-system/role/roleAdd";
	}
	
	@RequestMapping("/update/{roleId}/Index")
	public String updateH5Index(@PathVariable("roleId") String roleId,HttpServletRequest request){
		SysRoleVO sysRoleVO = roleService.findRoleById(roleId);
		request.setAttribute("sysRoleVO", sysRoleVO);
		return "view-system/role/roleUpdate";
	}
}
